package br.ucb.mercadojogador.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.mercadojogador.beans.Presidente;
import br.ucb.mercadojogador.beans.Time;
import br.ucb.mercadojogador.connection.FactoryConnection;

public class PresidenteDAOImpl implements PresidenteDAO{

	@Override
	public void insertPresidente(Presidente presidente) {
		
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "INSERT INTO PRESIDENTE VALUES (?,?,?);";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1,0);
			ps.setString(2,presidente.getNome());
			ps.setInt(3,presidente.getTime().getIdTime());
			ps.execute();		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
	}

	@Override
	public void deletePresidente(int idPres) {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "DELETE FROM PRESIDENTE WHERE idPres=?;";
		
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1,idPres);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
	}

	@Override
	public void updatePresidente(Presidente presidente) {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "UPDATE PRESIDENTE SET nome=?, time_idTime=? WHERE idPres=?;";
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1,presidente.getNome());
			ps.setInt(2,presidente.getTime().getIdTime());
			ps.setInt(3,presidente.getIdPres());
			ps.execute();		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		}
		
	}

	@Override
	public List<Presidente> findAllPresidentes() {
		Connection con = FactoryConnection.getConnection();
		PreparedStatement ps = null;
		String sql = "SELECT * FROM PRESIDENTE;";
		ArrayList<Presidente> times = new ArrayList<Presidente>();
		Presidente presidente = null;
		
		try {
			ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				presidente = new Presidente();
				putPresidente(rs,presidente);
				times.add(presidente);				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return times;
	}
	
	private void putPresidente(ResultSet rs, Presidente presidente) throws SQLException{
		presidente.setIdPres(rs.getInt("IDPRES"));
		presidente.setNome(rs.getString("NOME"));
		presidente.setTime(getTime(rs.getInt("TIME_IDTIME")));
	}
	
	private Time getTime(int idTime){
		TimeDAO timeDao = new TimeDAOImpl();
		Time time = timeDao.consultar(idTime);
		return time;
	}
	
	
}
